package com.microservice.core.aop;

import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;


/**
 * 记录请求路径和请求方法切面类(只记录controller层)
 */
@Aspect
@Component
public class RequestLoggingAspect {

    @Before("execution(* com.microservice.*.controller..*.*(..))")
    public void logRequest(JoinPoint joinPoint) {
        // 获取当前请求
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        // 打印请求路径
        String requestURL = request.getRequestURL().toString();
        System.out.println("请求路径: " + requestURL);

        // 打印方法签名
        System.out.println("方法签名: " + joinPoint.getSignature().toString());
    }
}
